package com.example.navigationstudy;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainFragment extends Fragment {

    private MainViewModel mViewModel;
    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mViewModel.setTip("你好，我是start ：MainFragment，点击按钮使用Navigaiton组件一行代码切换到新的fragment");
        // TODO: Use the ViewModel
        mViewModel.getTip().observe(getActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                TextView tvTip = getView().findViewById(R.id.tvTip);
                tvTip.setText(s);
            }
        });

        getView().findViewById(R.id.btToSecond).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new MainFragmentArgs.Builder()
                        .setUsername("andy")
                        .setAge(10).build().toBundle();
                Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_secondLoginFragment,bundle);
            }
        });


    }

}